package com.atguigu.controller;

import com.atguigu.controller.base.ModelBaseServlet;
import com.atguigu.pojo.*;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.JSONUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @Author luoyin
 * @Date 16:06 2022/7/21
 * @Description 描述
 **/
@WebServlet("/protect/cart")
public class CartController extends ModelBaseServlet {
    //去购物车页面
    public void toCartPage(HttpServletRequest request, HttpServletResponse response){

        try {
            processTemplate("pages/cart/cart",request,response);
        } catch (IOException e) {

        }
    }

    //查询购物车
    public void selectCart(HttpServletRequest request, HttpServletResponse response){
        Cart cart = (Cart) request.getSession().getAttribute(BookstoreConstant.SESSION_KEY_CART);
        ResultVO<Cart> resultVO = new ResultVO<>(true,"查询购物车成功",cart);
        try {
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //加入购物车
    public void addCartItem(HttpServletRequest request, HttpServletResponse response){

        /*//判断是否登录账户,如果没有登录,不能加入购物车
        Object existUser = request.getSession().getAttribute("existUser");
        if(null == existUser){
            try {
                response.sendRedirect(request.getContextPath()+"/user?method=toLoginPage");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }*/

        Integer bookId = Integer.valueOf(request.getParameter("bookId"));
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        BookService bookService = new BookServiceImpl();
        try {
            Book book = bookService.selectById(bookId);
            if (cart == null){
                cart = new Cart();
            }
            cart.addBook2CartItem(book);
            request.getSession().setAttribute("cart",cart);
            response.sendRedirect(request.getContextPath()+"/index.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //商品数量加一
    public void cartItemIncreaseCount(HttpServletRequest request, HttpServletResponse response){
        Integer bookId = Integer.valueOf(request.getParameter("bookId"));
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.cartItemIncreaseCount(bookId);
        ResultVO<Object> resultVO = new ResultVO<>(true,"购物车项数量加1成功",null);
        try {
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //商品数量减一
    public void cartItemDecreaseCount(HttpServletRequest request, HttpServletResponse response){
        Integer bookId = Integer.valueOf(request.getParameter("bookId"));
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.cartItemDecreaseCount(bookId);
        ResultVO<Object> resultVO = new ResultVO<>(true,"购物车项数量减1成功",null);
        try {
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //删除商品项
    public void deleteCartItem(HttpServletRequest request, HttpServletResponse response){
        Integer bookId = Integer.valueOf(request.getParameter("bookId"));
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        cart.removeCartItem(bookId);
        ResultVO<Object> resultVO = new ResultVO<>(true,"购物车项删除成功",null);
        try {
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //清空购物车
    public void removeCart(HttpServletRequest request, HttpServletResponse response){
        request.getSession().removeAttribute("cart");
        ResultVO<Object> resultVO = new ResultVO<>(true,"购物车清空成功",null);
        try {
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //修改数量
    public void updateCartItemCount(HttpServletRequest request, HttpServletResponse response){
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        Integer bookId = Integer.valueOf(request.getParameter("bookId"));
        Integer newCount = Integer.valueOf(request.getParameter("newCount"));
        cart.updateCartItemCount(bookId,newCount);
        ResultVO<Object> resultVO = new ResultVO<>(true,"购物车项数量修改成功",null);
        try {
            JSONUtils.object2JsonStrAndresponse(response,resultVO);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
